package km.TestFileExplorer;

import java.io.*;
import java.util.*;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.media.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class ExplorerListView extends ListView{

	private LayoutInflater infilter;
	private BaseAdapter adapter;
	private File[] files;
	private ImageView vIcon;
	private TextView vFileName;
	private TextView vFileSize;
	private Bitmap bitmap;
	private BitmapDrawable draw;
	private BitmapFactory.Options bfo = new BitmapFactory.Options();
	private HashMap<String, Drawable> imagePool;
	public HashMap<String,File> checkFiles;
	public ExplorerListView(Context context) {
		super(context);
		infilter = ((Activity)context).getLayoutInflater();
		bfo.inSampleSize = 1;
		if(imagePool == null)
			imagePool = new HashMap<String , Drawable> ();
		if(checkFiles == null)
		  checkFiles = new HashMap<String, File>();
		adapter =new ListAdapter();
		setAdapter(adapter);
	}
	public void initData(File[] FileDatas) {
		Log.e("FileDatas.length",FileDatas.length+"");
		files = FileDatas;	
		adapter.notifyDataSetChanged();
	}
	public class ListAdapter extends BaseAdapter {

		

		public int getCount() {
			return files==null ? 0 :files.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
//			MLog.e(this, "getView",position+"");
			if (convertView == null) {
				convertView = infilter.inflate(R.layout.row, null);
			}
			File file = files[position];
			String fname = file.getName();
			long fsize = file.length();
			String ext = fname.substring(fname.lastIndexOf('.')+1, fname.length());
			ext = ext.toLowerCase();
//			Drawable draw=Drawable.createFromPath(file.getPath());
			
			vIcon = (ImageView) convertView.findViewById(R.id.item_icon);
			vFileName = (TextView) convertView.findViewById(R.id.item_file_name);
			vFileSize = (TextView) convertView.findViewById(R.id.item_file_size);
//			MLog.e(this, "fileLength",file.length()+"");
//			MLog.e(this, "fileName",file.getName()+"::"+vText.isChecked());
			
			File checkFile = checkFiles.get(file.toString());
			if(checkFile==null){
				vFileName.setTextColor(Color.WHITE);
				vFileSize.setTextColor(Color.WHITE);
			}else{
				vFileName.setTextColor(Color.BLUE);
				vFileSize.setTextColor(Color.BLUE);
			}

				
			if(file.isDirectory()){
				vIcon.setBackgroundResource(R.drawable.icon_folder);
			}else if(ext.equals("xls") || ext.equals("xlsx")){
				vIcon.setBackgroundResource(R.drawable.icon_xls);
			}else if(ext.equals("doc") || ext.equals("docx")){
				vIcon.setBackgroundResource(R.drawable.icon_word);
			}else if(ext.equals("ppt") || ext.equals("pptx")){
				vIcon.setBackgroundResource(R.drawable.icon_ppt);
			}else if(ext.equals("jpg")||ext.equals("bmp")||ext.equals("png")){
				Log.e("File",file.toString());
				Log.e("File.length()",file.length()+"");

				if(imagePool.get(file.toString())==null){
			    	int width=0;
			    	int height=0;
			    	try {
						ExifInterface exif = new ExifInterface(file.toString());
						width=Integer.parseInt(exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH));
						height=Integer.parseInt(exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH));
						bfo.inSampleSize=height/50;
					     Log.e("","TAG_IMAGE_LENGTH >> "+width);
					     Log.e("","TAG_IMAGE_WIDTH >>"+height);
					} catch (IOException e) {
						e.printStackTrace();
						bfo.inSampleSize=1;
					}
					bitmap = BitmapFactory.decodeFile(file.getPath(),bfo);
					draw = new BitmapDrawable(bitmap);
					draw.setCallback(null);
					imagePool.put(file.toString(),draw );
//					MLog.d("bitmap.getWidth()>>>>>"+bitmap.getWidth());
					
				}else
					draw =(BitmapDrawable)imagePool.get(file.toString());	
				vIcon.setBackgroundDrawable(draw);
			}else
				vIcon.setBackgroundResource(R.drawable.icon_file);
			
			vFileName.setText(fname);
			if(file.isDirectory())
				vFileSize.setText("");	
			else
				vFileSize.setText(convertFormat(fsize));
//				vFileSize.setText(String.format("%,d", fsize)+"");			
			return convertView;
		}
	
	}
	
    public String convertFormat(long size){
		String result;
		try{
			if(size>=1000000000){			
				result = String.format("%,1.1f",size/1000000000f)+"GB";	
			}else if(size>=1000000){
				result = String.format("%1.1f",size/1000000f)+"MB";
			}else if(size>=1000){
				result = String.format("%1.1f",size/1000f)+"KB";
			}else if(size>0){
				result = String.format("%1.3f",size/1000f)+"KB";
			}else{
				result = "0KB";
			}
		}catch (Exception e) {
			e.printStackTrace();
			return size+"";
		}
		return result;
    }
	public void cleanUp(){
		if(bitmap!=null) bitmap.recycle();
			bitmap =null;
		if(imagePool!=null) imagePool.clear();
			imagePool =null;
		if(checkFiles!=null) checkFiles.clear();
			checkFiles =null;
		if(draw!=null) draw=null;
	}
}
